package com.wondertek.poms.service.controller;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.wondertek.poms.common.vo.ResultBean;
import com.wondertek.poms.core.service.IContentBlackService;
import com.wondertek.poms.core.service.IContentService;
import com.wondertek.poms.core.service.IContentWhiteService;
import com.wondertek.poms.core.service.IMoveContLogService;
import com.wondertek.poms.core.service.IPrdInfoService;
import com.wondertek.poms.core.service.IProductInfoPackageService;
import com.wondertek.poms.dao.po.Content;
import com.wondertek.poms.dao.po.ContentBlack;
import com.wondertek.poms.dao.po.ContentWhite;
import com.wondertek.poms.dao.po.MoveContLog;
import com.wondertek.poms.dao.po.PrdInfo;
import com.wondertek.poms.dao.po.ProductInfoPackage;
import com.wondertek.poms.dao.repo.IMoveContLogDao;

/**
 * @ClassName: AutoImportContentControl
 * @Description: TODO
 * @Author MrYuan
 * @Date: 2021/1/25 9:55
 * @Version 1.0
 */
@RestController
@Api(produces = "黑白名单导入")
public class AutoImportContentControl {
    @Autowired
    IContentBlackService blackService;
    @Autowired
    IContentWhiteService whiteService;
    @Autowired
    IMoveContLogService moveContLogService;
    @Autowired
    IMoveContLogDao moveContLogDao;
    @Autowired
    IPrdInfoService prdInfoService;
    @Autowired
    IContentService contentService;
    @Autowired
    IProductInfoPackageService productInfoPackageService;


    /**
     * @Author MrYuan
     * @Date: 2021/1/26 13:44
     * @Description: 导入工作启动入口
     */
    @ApiOperation(value = "", produces = "导入工作启动入口")
    @GetMapping("start")
    public ResultBean getStart() {
        long size = moveContLogDao.count();
        int page = (int) Math.ceil(size * 1.0 / 1000);
        int count = 1;
        while (count <= page) {
            autoImportBlank(count);
            count += 1;
        }
        return ResultBean.ok();
    }


    /**
     * @Author MrYuan
     * @Date: 2021/1/25 16:43
     * @Description: 根据传入的日期，将大于该日期的数据进行导入工作
     */
    @ApiOperation(value = "可以根据传入日期进行定时导入")
    @GetMapping("importByTime")
    public void importByTime(@RequestParam int minute) {
        Calendar instance = Calendar.getInstance();
        Date endTime = instance.getTime();
        instance.add(Calendar.MINUTE, -minute);
        Date startTime = instance.getTime();
        List<MoveContLog> moveContLogsByTime = moveContLogDao.findAllByCreateTimeBetweenOrderByIdDesc(startTime, endTime);
        if (!CollectionUtils.isEmpty(moveContLogsByTime)) {
            implement(moveContLogsByTime);
        }
    }

    /**
     * @Author MrYuan
     * @Date: 2021/1/25 10:21
     * @Description: 自动导入黑白名单数据
     */
    public ResultBean autoImportBlank(
            int count) {
        int page = count;
        int rows = 1000;
        String sidx = "createTime";
        String sord = "desc";
        Page<MoveContLog> m = moveContLogService.findAll(page, rows, sord, sidx);
        List<MoveContLog> moveContLogs = m.getContent();
        if (!CollectionUtils.isEmpty(moveContLogs)) {
            implement(moveContLogs);
        }
        return ResultBean.ok();
    }

    /*/**
     * @Author MrYuan
     * @Date: 2021/2/20 10:30
     * @Description: 导入工作的具体实现
     * @params:  [moveContLogs]
     * @return: void
     */
    public void implement(List<MoveContLog> moveContLogs) {
        for (int i = 0; i < moveContLogs.size(); i++) {
            try {
                MoveContLog moveContLog = moveContLogs.get(i);
                String contentId = moveContLog.getContentId();
                Long oldPrdInfoId = moveContLog.getOldPrdInfoId();
                Long newPrdInfoId = moveContLog.getNewPrdInfoId();
                if (oldPrdInfoId != null) {
                    ContentBlack contentBlack = new ContentBlack();
                    contentBlack.setPrdInfoId(oldPrdInfoId);
                    contentBlack.setContentId(moveContLog.getContentId());
                    contentBlack.setContentName(moveContLog.getContentName());
                    if (searchIsEmpty(contentBlack)) {
                        ContentBlack ctb = new ContentBlack();
                        ctb.setContentId(contentId);
                        ctb.setPrdInfoId(oldPrdInfoId);
                        List<ContentBlack> contentBlackList = blackService.findAll(ctb);
                        if (CollectionUtils.isEmpty(contentBlackList)) {
                            blackService.save(contentBlack);
                        }
                    } else {
                        if (isIncludPrdInfoBlank(contentBlack.getPackageId(), contentId)) {
                            blackService.save(contentBlack);
                        }
                    }
                }
                if (newPrdInfoId != null) {
                    ContentWhite contentWhite = new ContentWhite();
                    contentWhite.setPrdInfoId(newPrdInfoId);
                    contentWhite.setContentId(moveContLog.getContentId());
                    contentWhite.setContentName(moveContLog.getContentName());
                    if (searchIsEmpty(contentWhite)) {
                        ContentWhite white = new ContentWhite();
                        white.setContentId(contentId);
                        white.setPrdInfoId(newPrdInfoId);
                        List<ContentWhite> contentWhites = whiteService.findAll(white);
                        if (CollectionUtils.isEmpty(contentWhites)) {
                            whiteService.save(contentWhite);
                        }
                    } else {
                        if (isIncludPrdInfoWhite(contentWhite.getPackageId(), contentId)) {
                            whiteService.save(contentWhite);
                        }
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * @Author MrYuan
     * @Date: 2021/1/25 10:17
     * @Description: 判断二级商品id是否为空(黑名单对象赋值)
     */
    public boolean searchIsEmpty(ContentBlack black) {
        Content byContentId = contentService.findByContentId(black.getContentId());
        if (!ObjectUtils.isEmpty(byContentId)) {
            black.setContentName(byContentId.getName());
        }
        PrdInfo one = prdInfoService.findByPrdInfoId(black.getPrdInfoId());
        if (!ObjectUtils.isEmpty(one)) {
            Long twoId = one.getProductInfoPackageId();
            black.setPrdInfoName(one.getName());
            if (twoId != null) {
                black.setPackageId(twoId);
                ProductInfoPackage byId = productInfoPackageService.findById(twoId);
                if (!ObjectUtils.isEmpty(byId)) {
                    black.setPackageName(byId.getProductInfoPackageName());
                }
                return false;
            }
        }
        return true;
    }

    /**
     * @Author MrYuan
     * @Date: 2021/1/25 10:17
     * @Description: 判断二级商品id是否为空(白名单对象赋值)
     */
    public boolean searchIsEmpty(ContentWhite white) {
        Content byContentId = contentService.findByContentId(white.getContentId());
        if (!ObjectUtils.isEmpty(byContentId)) {
            white.setContentName(byContentId.getName());
        }
        PrdInfo one = prdInfoService.findByPrdInfoId(white.getPrdInfoId());
        if (!ObjectUtils.isEmpty(one)) {
            Long twoId = one.getProductInfoPackageId();
            white.setPrdInfoName(one.getName());
            if (twoId != null) {
                white.setPackageId(twoId);
                ProductInfoPackage byId = productInfoPackageService.findById(twoId);
                if (!ObjectUtils.isEmpty(byId)) {
                    white.setPackageName(byId.getProductInfoPackageName());
                }
                return false;
            }
        }
        return true;
    }

    /**
     * @Author MrYuan
     * @Date: 2021/1/25 10:18
     * @Description:判断黑名单数据是否在二级产品下面的其他商品包中
     */
    public boolean isIncludPrdInfoBlank(Long twoid, String contentId) {
        List<PrdInfo> prdInfoList = prdInfoService.findPrdInfoInPackageId(twoid, 2);
        ContentBlack black = new ContentBlack();
        black.setContentId(contentId);
        List<ContentBlack> contentBlackList = blackService.findAll(black);
        if (!CollectionUtils.isEmpty(prdInfoList)) {
            if (!CollectionUtils.isEmpty(contentBlackList)) {
                for (PrdInfo prdInfo : prdInfoList) {
                    for (ContentBlack contentBlack : contentBlackList) {
                        if (prdInfo.getPrdInfoId().longValue() == contentBlack.getPrdInfoId().longValue()) {
                            return false;
                        }
                    }
                }
            }
        }
        return true;
    }

    /**
     * @Author MrYuan
     * @Date: 2021/1/25 10:18
     * @Description:判断白名单数据是否在二级产品下面的其他商品包中
     */
    public boolean isIncludPrdInfoWhite(Long twoid, String contentId) {
        List<PrdInfo> prdInfoList = prdInfoService.findPrdInfoInPackageId(twoid, 2);
        List<ContentWhite> contentWhites = whiteService.findAll(new ContentWhite().setContentId(contentId));
        if (!CollectionUtils.isEmpty(prdInfoList)) {
            if (!CollectionUtils.isEmpty(contentWhites)) {
                for (PrdInfo prdInfo : prdInfoList) {
                    for (ContentWhite contentWhite : contentWhites) {
                        if (prdInfo.getPrdInfoId().longValue() == contentWhite.getPrdInfoId().longValue()) {
                            return false;
                        }
                    }
                }
            }
        }
        return true;
    }

}
